/*
Copyright (c) 2006 eBay, Inc.

This program is licensed under the terms of the eBay Common Development and 
Distribution License (CDDL) Version 1.0 (the "License") and any subsequent 
version thereof released by eBay.  The then-current version of the License 
can be found at https://www.codebase.ebay.com/Licenses.html and in the 
eBaySDKLicense file that is under the eBay SDK install directory.
 */
package consoleviewitem;

import java.io.*;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.*;
import javax.xml.parsers.*;

import org.w3c.dom.Document;

public class ApplicationViewItem {

    /**
     * Constructs a Document object by reading from an input stream.
     */
    public static Document parse(InputStream is) {
        Document ret = null;
        DocumentBuilderFactory domFactory;
        DocumentBuilder builder;

        try {
            domFactory = DocumentBuilderFactory.newInstance();
            domFactory.setValidating(false);
            domFactory.setNamespaceAware(false);
            builder = domFactory.newDocumentBuilder();

            ret = builder.parse(is);
        } catch (Exception ex) {
            System.err.println("unable to load XML: " + ex);
        }
        return ret;
    }
    //Main method

    public static void main(String[] args) {
        InputStream in = null;
        try {
            String url = "http://svcs.ebay.com/services/search/FindingService/v1?OPERATION-NAME=findItemsByKeywords&SERVICE-VERSION=1.0.0&SECURITY-APPNAME=MaxPassa-e76b-4a6d-ba28-eee669c9b9b9&RESPONSE-DATA-FORMAT=XML&REST-PAYLOAD&keywords=harry%20potter%20phoenix&paginationInput.entriesPerPage=2";
            URL xmlUrl = new URL(url);
            in = xmlUrl.openStream();
            Document doc = parse(in);
            // normalize text representation
            doc.getDocumentElement().normalize();
            System.out.println("Root element of the doc is " + doc.getDocumentElement().getNodeName());
        } catch (IOException ex) {
            Logger.getLogger(ApplicationViewItem.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                in.close();
            } catch (IOException ex) {
                Logger.getLogger(ApplicationViewItem.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }
}
